/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package domain;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javamines.Game;
import javamines.model.User;

/**
 *
 * @author Lucas
 */
public class Server {

 
     private ServerSocket socket;
     private User waitingPlayer;
     private final int poort = 1234;
     private HashMap<String, Connection> map;
     private Random r;


     public Server() throws IOException{
        try{
             socket = new ServerSocket(poort);
             map = new HashMap<String, Connection>();
        }
        catch(IOException e){
            System.err.println(e);
        }

     }
     public void run(){
          while(true){
            try {
                Connection conn = new Connection(socket.accept(), "Game " + r.nextInt(10));
                map.put(conn.getNaam(),conn );
                conn.start();

            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
         }
     }
     public synchronized Game waitForGame(User u){
         Game m = null;
         if(waitingPlayer == null){
             waitingPlayer = u;
         }
         else
            m = new Game(waitingPlayer, u);
            waitingPlayer = null;
            notify();
            return m;
     }

}



